package com.houmeiyuan.huiguniang.ctrl;

import com.houmeiyuan.huiguniang.common.Result;
import com.houmeiyuan.huiguniang.modles.domain.ShopCar;
import com.houmeiyuan.huiguniang.modles.domain.ShopCarDetail;
import com.houmeiyuan.huiguniang.service.ProductService;
import com.houmeiyuan.huiguniang.service.ShopCarService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.*;

@RestController
@RequestMapping("/shopcar")
public class ShopCarCtrl extends BaseCtrl {
    @Autowired
    private ShopCar shopCar ;

    @Autowired
    public ShopCarService shopCarService;
    @Autowired
    public ProductService productService;
    private static final Logger LOG = LoggerFactory.getLogger(ShopCarCtrl.class);

    /**
     * 测试这一层是否服务开启（测试用代码）
     * 2019-08-21
     * lilanlan
     *
     * @return
     */
    @RequestMapping("/chan")
    public Result hi() {

        Result result = new Result();
        result.setResult("1");
        return result;
    }

    /**
     * fanfan
     * 用户向购物车中添加商品
     *
     * @param openId
     * @param productId
     * @param productNumber
     * @return
     */
    @PostMapping("/addProduct")
    public Result addProduct(@RequestParam("openId") String openId,
                             @RequestParam("productId") Integer productId,
                             @RequestParam("productNumber") Integer productNumber) {
        Map<String, Object> mp = new HashMap<>();

        if (openId != null && !"".equals(openId)) {
            mp.put("openId", openId);
        }
        if (productId != null && !"".equals(productId)) {
            mp.put("productId", productId);
            mp.put("productName", productService.selectById(productId).getProductName());
            mp.put("productPrice", productService.selectById(productId).getProductPrice());
            mp.put("updateTime", new Date());
        }
        if (productNumber != null && !"".equals(productNumber)) {
            mp.put("productNumber", productNumber);
        }
        System.out.println(productService.selectById(productId));
        if (shopCarService.checkProduct(mp) != 0) {
            /*商品已经存在于购物车直接在原有的基础上继续添加数量*/
            return send(shopCarService.updateProduct2(mp));
        } else {
            /*添加的商品未出现在购物车 重新插入一条新的记录*/
            return send(shopCarService.addProduct(mp));
        }
    }

    /**
     * @author lilanlan
     * @date 2019-08-11 20:09
     */
    @RequestMapping("/selectAll")
    public Result selectAll(@RequestParam(name = "openId") String openId) {
        Map<String, Object> params = new HashMap<>();
        if (openId != null && !"".equals(openId)) {
            params.put("openId", openId);
        }
        List<ShopCarDetail> shopCarDetailList = new ArrayList<ShopCarDetail>();

        try {
            shopCarDetailList = (List<ShopCarDetail>) shopCarService.selectAll(params);
        } catch (Exception e) {
            LOG.info(e.toString());
        }

        LOG.info("1");

        return send(shopCarDetailList);
    }

    /**
     * @author lilanlan
     * @date 2019-08-14 20:09
     */
    @DeleteMapping("/deleteProduct")
    public Result deleteShopCar(@RequestParam(name = "openId") String openId,//用户Id是openId,注意是String类型
                                @RequestParam(name = "productId") Integer productId) {
        Map<String, Object> mp = new HashMap<>();
        if (openId != null && !"".equals(openId)) {
            mp.put("openId", openId);
        }
        if (productId != null && !"".equals(productId)) {
            mp.put("productId", productId);
        }
        return send(shopCarService.deleteProduct(mp));
    }

    /**
     * 当前商品选中事件
     * @return
     * @author nkz
     * @author lll
     */
//    @PutMapping("/updateShopCar")
//    public Result updateShopCar(@RequestBody(required = false) String str) {
//        try {
//            JSONObject jsonObject = JSON.parseObject(str);
//            String openId = jsonObject.getString("openId");
//            Integer productId = jsonObject.getInteger("productId");
//            Integer productNumber = jsonObject.getInteger("productNumber");
//            Integer selected = jsonObject.getInteger("selected");
//            Map<String, Object> params = new HashMap<>();
//            if (openId != null && !"".equals(openId)) {
//                params.put("openId", openId);
//            }
//            if (productId != null && !"".equals(productId)) {
//                params.put("productId", productId);
//            }
//            if (selected != null&& !"".equals(selected) ) {
//                params.put("selected", selected);
//            }
//            if (productNumber != null&& !"".equals(productNumber)) {
//                params.put("productNumber", productNumber);
//            }
//            return send(shopCarService.updateShopCar(params), 200, "操作成功!");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return send(-1, "操作失败!");
//        }
//    }


    /**
     * @param openId
     * @param productId
     * @param productNumber
     * @param selected
     * @return
     * @author lilanlan
     * 绑定加数量事件：增加数据库中对应商品id的数量1个
     * 加事件就更新传的数量的值就行了，前端已经实现加1了(2019-08-26)
     */
    @PutMapping("/updateShopCar")
    public Result updateShopCar(@RequestParam(name = "openId") String openId,//注意是String类型
                                @RequestParam(name = "productId") Integer productId,
                                @RequestParam(name = "productNumber", required = false) Integer productNumber,
                                @RequestParam(name = "selected") Integer selected) {
        try {
            Map<String, Object> params = new HashMap<>();
        if (openId != null && !"".equals(openId)) {
            params.put("openId", openId);
        }
        if (productId != null && !"".equals(productId)) {
            params.put("productId", productId);
        }
        if (selected != null && !"".equals(selected)) {
            params.put("selected", selected);
        }
        if (productNumber != null && !"".equals(productNumber)) {
            params.put("productNumber", productNumber);
            Date date = new Date();
            params.put("updateTime", date);
        }
//        return send(shopCarService.updateShopCar(params));
        return send(shopCarService.updateShopCar(params), 200, "操作成功!");
    } catch(Exception e)
    {
        e.printStackTrace();
        return send(-1, "操作失败!");
    }
    }

    /**
     * 购物车全选事件
     *
     * @return
     * @author nkz
     */
    @PostMapping("/allSelect")
    public Result allSelect(@RequestParam(name = "openId") String openId,//注意是String类型
                            @RequestParam(name = "selected") Integer selected) {
        try {
            //JSONObject jsonObject = JSON.parseObject(str);
            //System.out.println(jsonObject);
            //String openId = jsonObject.getString("openId");
            //Integer selected = jsonObject.getInteger("selected");
            //Map<String, Object> params = new HashMap<>();

            if (openId != null && !"".equals(openId)) {
               shopCar.setOpenId(openId);
            }
            if (selected != null && !"".equals(selected)) {
               shopCar.setSelected(selected);
            }
            return send(shopCarService.allSelect(shopCar), 200, "操作成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return send(-1, "操作失败!");
        }
    }


}
